<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use app\common\model\Category as CategoryModel;
use app\common\model\Tags as TagsModel;
use app\common\model\Goodlink as GoodlinkModel;
use app\common\model\Article as ArticleModel;
use think\Cache;

class common extends Controller
{
    public function _initialize()
    {
        //分类
        $categorys = unserialize(Cache::remember('categorys', function () {
            //组装遍历 文章数目
            return serialize(CategoryModel::all());
        }));
        //标签
        $tags = unserialize(Cache::remember('tags', function () {
            return serialize(TagsModel::all());
        }));
        //友情链接
        $links = unserialize(Cache::remember('links', function () {
            return serialize(GoodlinkModel::all());
        }));
        //最新文章 取最新的2篇
        $newArticles = unserialize(Cache::remember('newArticles', function () {
            return serialize(ArticleModel::all(function ($query) {
                $query->where('softdelete', 0)->limit(2)->order('time', 'desc');
            }));
        }));

        $this->assign(compact('categorys', 'tags', 'newArticles', 'links'));
    }
}
